#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>

int a[64] = 
{
	494, 3931, 30071, 12903, 7803, 24454, 701, 17278, 10682, 18047, 26361, 12702, 23587, 
	20776, 6286, 619, 10842, 20712, 21852, 9748, 1633, 26633, 3476, 1753, 16653, 13663, 
	28875, 12746, 8118, 25387, 20814, 31953, 31283, 7486, 20918, 19954, 29957, 12898, 
	8196, 14682, 24019, 27881, 8280, 8553, 21451, 17620, 7771, 31825, 4477, 14533, 29371, 
	18654, 16403, 7304, 29726, 19688, 28823, 21842, 27134, 3286, 17766, 11612, 8631, 23649
};
int b[64];

void mergesort(int l,int r)
{
	if(l == r)return;
	int mid = (l+r)/2;
	mergesort(l,mid);
	mergesort(mid+1,r);
	int i = l,ie = mid,j = mid+1,je = r,m,k = l;
	for(m = l;m<=r;++m)
	{
		b[m] = a[m];	
	}
	while(i<=ie && j<=je)
	{
		if(b[i]<b[j])
		{
			a[k++] = b[i++];
		}
		else
		{
			a[k++] = b[j++];
		}
	}
	while(i<=ie)a[k++] = b[i++];
	while(j<=je)a[k++] = b[j++];
}

int main(void)
{
	__asm__ __volatile__("int $0x90"::"a"(0x10000006), "b"(2));
	open("/dev/tty0", 0, 0);
	open("/dev/tty0", 0, 0);
	
	printf("Welcome to NewLife.\n");
	printf("nfsh v1.0 started.\n");
	time_t tm = time(0);
	printf("Current time: %s", ctime(&tm));
	char cmd[64];
	while(1)
	{
		printf(">");
		scanf("%s", cmd);
		if(strcmp(cmd, "time")==0)
		{
			tm = time(0);
			printf("%s", ctime(&tm));
		}
		else if(strcmp(cmd, "sort")==0)
		{
			int i;

			srand(time(0));
			for(i=0; i<=64; i++)
				a[i] = rand()%100;	

			for(i=0; i<64; i++)
				printf("%d, ", a[i]);
			printf("%d\n", a[i]);

			printf("Sort result:\n");
			char ch;
			scanf("%c", &ch);
			mergesort(0,64);
			for(i=0; i<64; i++)
				printf("%d, ", a[i]);
			printf("%d\n", a[i]);
		}
		else
			printf("Bad file or command: %s\n", cmd);
	}

	while(1);
}

